package alf.mapping.core;

public class Vector2d {

	float [] valores = {0,0};

	private static final int X = 0;
	private static final int Y = 1;
	private static final float ZERO = 0f;

	public Vector2d() {
		
	}
	
	
	public Vector2d(float x,float y) {
		valores[X] = x;
		valores[Y] = y;
	}

	public void substract(Vector2d otro) {
		valores[X] -= otro.valores[X];
		valores[Y] -= otro.valores[Y];
	}

	public void setY(float x) {
		valores[X] = x;
	}

	public void setX(float y) {
		valores[Y] = y;
	}
	
	public void scale(float factor) {
		valores[X] *= factor;
		valores[Y] *= factor;
	}

	public Vector2d clone() {
		Vector2d r = new Vector2d();
		r.setValores(this);
		return r;
	}

	public void add(Vector2d v2) {
		valores[X] += v2.valores[X];
		valores[Y] += v2.valores[Y];
	}
	public void setValores(float x,float y) {
		this.valores[0] = x;
		this.valores[1] = y;
	}
	
	public void setValores(float[] valores) {
		this.valores[0] = valores[0];
		this.valores[1] = valores[1];
	}
	public void setValores(Vector2d v) {
		this.valores[X] = v.valores[X];
		this.valores[Y] = v.valores[Y];
	}
	public void decrease(float roce, float umbral) {
		for (int i=0;i < 2;i++) {
			float v = valores[i];
			if (v != ZERO) {
				v *= 	1 - (roce * Math.abs(v));
				if (Math.abs(v) < umbral) {
					v = ZERO;
				}
			}
			valores[i] = v;
		}
	}
	
	public String toString(){
		return "(" + getX() + "," + getY()+ ")";
	}
	public float[] getValores() {
		return valores;
	}

	public float getX() {
		return valores[X];
	}
	public float getY() {
		return valores[Y];
	}

	public float distance(Vector2d otro) {
	//	Vector2d tmp = this.clone();
		return (float) Math.sqrt(
			(valores[X]-otro.valores[X])
			*(valores[X]-otro.valores[X])
			+ (valores[Y]-otro.valores[Y]) 
			*(valores[Y]-otro.valores[Y]));
	}
	
	public float length() {
		return (float) Math.sqrt(valores[X]*valores[X]
			+ valores[Y] * valores[Y] );
	}
}
